package ServletDemo;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class noLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端以UTF-8编码进行输出
        resp.setCharacterEncoding("UTF-8");
        //设置浏览器以UTF-8编码进行接收,解决中文乱码问题
        resp.setContentType("text/html;charset=UTF-8");
        String userName = req.getParameter("username");
        String mima = req.getParameter("passwd");
        String keep = req.getParameter("keep");//查看是选中keep复选框
        if (userName.equals(mima)) {
            if (keep != null) {
                Cookie name = new Cookie("name", userName);
                Cookie passWd = new Cookie("passWd", mima);
                name.setMaxAge(14 * 24 * 3600);
                resp.addCookie(name);
                passWd.setMaxAge(14 * 24 * 3600);
                resp.addCookie(passWd);
//                resp.sendRedirect("show?posturl="+ userName);
                resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/successCookies.jsp");
            }

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
